import { HashRouter as Router, Switch, Route, Redirect } from "react-router-dom";
import ClientRouter from '../router/ClientPageRouter'
import {getStorage} from '../utils/utils'

export const RouterOption = () => {
    let User = getStorage('user')
    let renderClientRouters = (list) => {
        return list.map((item) => {
            if(item.meta.isLogin){
                console.log(item)
                if(User){
                    if(item.children){
                        return <Route key={item.name} path={item.path} render={() => 
                            <item.component>
                                {renderClientRouters(item.children)}
                            </item.component>
                        }>
                        </Route>
                    }else{
                        return <Route key={item.name} path={item.path} component={item.component}/>
                    }
                }else{
                    return <Redirect name={item.name} to='/Login' />
                }
            }
            else if(item.redirect){
                return <Route key={item.name} exact path={item.path} render={() => <Redirect to={item.redirect} />} />
            }
            else{
                if(item.children){
                    return <Route key={item.name} path={item.path} render={() => 
                        <item.component>
                            {renderClientRouters(item.children)}
                        </item.component>
                    }>
                    </Route>
                }else{
                    return <Route key={item.name} path={item.path} component={item.component}/>
                }
            }
        })
    }
    return (
        <Router>
            <Switch>
                {renderClientRouters(ClientRouter)}
            </Switch>
        </Router>
    )
}